﻿/*
 * Author:xmfdsh 11届网络部--邓世豪
 * DataTime：2014-6-9
 * Copyright © 2014 大学生勤工助学服务队 Powered by xmfdsh & 四小狼工作室.
 * 版权所有，违者必究
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;

namespace QGZX.Helper
{
    public class GetSelfClass
    {

        public static string GetJson(List<MODEL.qgoa_user> list)
        {
            MODEL.qgzx_config config = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName == "系统设置").FirstOrDefault();
            //MODEL.qgoa_user user = new MODEL.qgoa_user();
            string[] list_string;

            string[,] day = new string[92, 2];
            for (int i = 0; i < 90; i++)
            {
                day[i, 0] = i.ToString();
            }
                for (int i = 0; i < list.Count; i++)
                {
                    string json = list[i].Json;
                    //判断json是否有问题
                    if (json.Length < 2)
                    {

                        continue;
                    }
                    list_string = json.Split(',');
                    for (int k = 0; k < list_string.Length; k++)
                    {

                        for (int j = 0; j < day.GetLength(0); j++)
                        {
                            if (list_string[k].ToString().Equals(day[j, 0]))
                                day[j, 1] += "0";
                        }

                    }
                    //把数组中的标记 0去掉，并追加
                    for (int n = 0; n < day.GetLength(0); n++)
                    {
                        if (day[n, 1] != null && day[n, 1].Length != 0)
                        {
                            if (day[n, 1].Substring(day[n, 1].Length - 1, 1).Equals("0"))
                            {
                                day[n, 1] = day[n, 1].Replace("0", "");
                                day[n, 1] = day[n, 1] + list[i].Name + ",";

                            }
                            else
                            day[n, 1] = day[n, 1].Replace("0", "");
                        }

                    }


                }

             MODEL.SelfClass selfclass = new MODEL.SelfClass();
            //赋值，最笨的方法，要改进啊，实在想不到什么好的循环赋值方法

             for (int i = 0; i < 90; i++)
             {
                 if (day[i, 1] == null)
                     day[i, 1] = " ";
             }
            //赋值，这是本系统最为垃圾恶心的地方，个人认为这里需要想方法来快速循环赋值，比如全部存放到List<string>泛型里面去来进行循环赋值，，等等。。。很多方法。
             selfclass.class0 = day[0, 1];
             selfclass.class1 = day[1, 1];
             selfclass.class2 = day[2, 1];
             selfclass.class3 = day[3, 1];
             selfclass.class4 = day[4, 1]; 
             selfclass.class5 = day[5, 1];
             selfclass.class6 = day[6, 1];
             selfclass.class7 = day[7, 1];
             selfclass.class8 = day[8, 1];
             selfclass.class9 = day[9, 1];
             selfclass.class10 = day[10, 1];
             selfclass.class11 = day[11, 1];
             selfclass.class12 = day[12, 1];
             selfclass.class13 = day[13, 1];
             selfclass.class14 = day[14, 1];
             selfclass.class15 = day[15, 1];
             selfclass.class16 = day[16, 1];
             selfclass.class17 = day[17, 1];
             selfclass.class18 = day[18, 1];
             selfclass.class19 = day[19, 1];
             selfclass.class20 = day[20, 1];
             selfclass.class21 = day[21, 1];
             selfclass.class22 = day[22, 1];
             selfclass.class23 = day[23, 1];
             selfclass.class24 = day[24, 1];
             selfclass.class25 = day[25, 1];
             selfclass.class26 = day[26, 1];
             selfclass.class27 = day[27, 1];
             selfclass.class28 = day[28, 1];
             selfclass.class29 = day[29, 1];
             selfclass.class30 = day[30, 1];
             selfclass.class31 = day[31, 1];
             selfclass.class32 = day[32, 1];
             selfclass.class33 = day[33, 1];
             selfclass.class34 = day[34, 1];
             selfclass.class35 = day[35, 1];
             selfclass.class36 = day[36, 1];
             selfclass.class37 = day[37, 1];
             selfclass.class38 = day[38, 1];
             selfclass.class39 = day[39, 1];
             selfclass.class40 = day[40, 1];
             selfclass.class41 = day[41, 1];
             selfclass.class42 = day[42, 1];
             selfclass.class43 = day[43, 1];
             selfclass.class44 = day[44, 1];
             selfclass.class45 = day[45, 1];
             selfclass.class46 = day[46, 1];
             selfclass.class47 = day[47, 1];
             selfclass.class48 = day[48, 1];
             selfclass.class49 = day[49, 1];
             selfclass.class50 = day[50, 1];
             selfclass.class51 = day[51, 1];
             selfclass.class52 = day[52, 1];
             selfclass.class53 = day[53, 1];
             selfclass.class54 = day[54, 1];
             selfclass.class55 = day[55, 1];
             selfclass.class56 = day[56, 1];
             selfclass.class57 = day[57, 1];
             selfclass.class58 = day[58, 1];
             selfclass.class59 = day[59, 1];
             selfclass.class60 = day[60, 1];
             selfclass.class61 = day[61, 1];
             selfclass.class62 = day[62, 1];
             selfclass.class63 = day[63, 1];
             selfclass.class64 = day[64, 1];
             selfclass.class65 = day[65, 1];
             selfclass.class66 = day[66, 1];
             selfclass.class67 = day[67, 1];
             selfclass.class68 = day[68, 1]; 
             selfclass.class69 = day[69, 1];
             selfclass.class70 = day[70, 1];
             selfclass.class71 = day[71, 1];
             selfclass.class72 = day[72, 1];
             selfclass.class73 = day[73, 1];
             selfclass.class74 = day[74, 1];
             selfclass.class75 = day[75, 1];
             selfclass.class76 = day[76, 1];
             selfclass.class77 = day[77, 1];
             selfclass.class78 = day[78, 1];
             selfclass.class79 = day[79, 1];
             selfclass.class80 = day[80, 1];
             selfclass.class81 = day[81, 1];
             selfclass.class82 = day[82, 1];
             selfclass.class83 = day[83, 1];
             selfclass.class84 = day[84, 1];
             selfclass.class85 = day[85, 1];

             selfclass.class86 = day[86, 1];
             selfclass.class87 = day[87, 1];
             selfclass.class88 = day[88, 1];
             selfclass.class89 = day[89, 1];

            //初始化，其实到这里的时候我很混乱了，所以用了很笨的方法，可以改进
            string aj="{\"class0\":\",\",\"class1\":\",\",\"class10\":\" \",\"class11\":\" \",\"class12\":\" \",\"class13\":\" \",\"class14\":\" \",\"class15\":\" \",\"class16\":\" \",\"class17\":\" \",\"class18\":\" \",\"class19\":\" \",\"class2\":\",\",\"class20\":\" \",\"class21\":\" \",\"class22\":\" \",\"class23\":\" \",\"class24\":\" \",\"class25\":\" \",\"class26\":\" \",\"class27\":\" \",\"class28\":\" \",\"class29\":\" \",\"class3\":\",\",\"class30\":\",\",\"class31\":\",\",\"class32\":\",\",\"class33\":\",\",\"class34\":\",\",\"class35\":\",\",\"class36\":\" \",\"class37\":\" \",\"class38\":\" \",\"class39\":\" \",\"class4\":\",\",\"class40\":\" \",\"class41\":\" \",\"class42\":\" \",\"class43\":\" \",\"class44\":\" \",\"class45\":\" \",\"class46\":\" \",\"class47\":\" \",\"class48\":\" \",\"class49\":\" \",\"class5\":\",\",\"class50\":\" \",\"class51\":\" \",\"class52\":\" \",\"class53\":\" \",\"class54\":\" \",\"class55\":\" \",\"class56\":\" \",\"class57\":\" \",\"class58\":\" \",\"class59\":\" \",\"class6\":\" \",\"class60\":\" \",\"class61\":\" \",\"class62\":\" \",\"class63\":\" \",\"class64\":\" \",\"class65\":\" \",\"class66\":\" \",\"class67\":\" \",\"class68\":\" \",\"class69\":\" \",\"class7\":\" \",\"class70\":\" \",\"class71\":\" \",\"class72\":\" \",\"class73\":\" \",\"class74\":\" \",\"class75\":\" \",\"class76\":\" \",\"class77\":\" \",\"class78\":\" \",\"class79\":\" \",\"class8\":\" \",\"class80\":\" \",\"class81\":\" \",\"class82\":\" \",\"class83\":\" \",\"class84\":\" \",\"class85\":\" \",\"class86\":\" \",\"class87\":\" \",\"class88\":\" \",\"class89\":\" \",\"class9\":\" \"}";
             string test = ObjectToJson(selfclass);
             if (test.Equals(""))
                 return aj;
             else
                 return test;
        }



        public static string ObjectToJson(object obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, obj);
            byte[] dataBytes = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(dataBytes, 0, (int)stream.Length);
            return Encoding.UTF8.GetString(dataBytes);
        }
    }
}